package com.yuting.server;

import com.yuting.FileTools.StoreAndReadFromFile;
import com.yuting.model.Question;

import java.util.*;

/**
 * @author cyt
 * @date 2020/7/2
 */
public class QuestionManagement implements ManageModule<Question> {
    private List<Question> library;


    private final static String LIBRARY_ADDRESS = "D:\\library.txt";
    /**
     * add a question into the library
     * @param question
     * @return
     */
    @Override
    public boolean add(Question question) {
        for (Question q : library){
            if(q.getQuestionNum() == question.getQuestionNum()){
                System.out.println("该题目编号已存在，请重新修改题目编号！");
                return false;
            }
        }

        library.add(question);
        sortLibary();
        return true;
    }

    /**
     * delete a question from library according to the questionNum
     * @param question
     * @return
     */
    @Override
    public boolean delete(Question question) {
        ListIterator listIterator = library.listIterator();
        while (listIterator.hasNext()){
            Question temp = (Question) listIterator.next();
            if(temp.getQuestionNum() == question.getQuestionNum()){
                listIterator.remove();
            }
        }
        return true;
    }


    /**
     * modify a question
     * @param question
     * @return
     */
    @Override
    public boolean modify(Question question) {
        //先查看是否存在此题号
        int count = 0;
        for(int i = 0; i <= library.size() - 1; i++){
            if(library.get(i).getQuestionNum() == question.getQuestionNum()){
                library.get(i).setQuestionContent(question.getQuestionContent());
                library.get(i).setCorrectAnswer(question.getCorrectAnswer());
                count++;
            }
        }
        if(count == 0){
            System.out.println("该题号不存在，请先添加该题目！");
            return false;
        }

        return true;
    }

    /**
     * search a question in terms of its question number
     * @param question
     * @return
     */
    @Override
    public Question search(Question question) {
        for(Question q : library){
            if(q.getQuestionNum() == question.getQuestionNum()){
                return q;
            }
        }
        System.out.println("该题号不存在，请先添加该题目！");
        return null;
    }

    /**
     * store the question library into a file
     */
    @Override
    public void storeData() {
        StoreAndReadFromFile.InfoStoring(library, LIBRARY_ADDRESS);
    }

    /**
     * read the library from a file
     */
    @Override
    public void readData() {
        this.library = (List<Question>)StoreAndReadFromFile.readInfo(LIBRARY_ADDRESS);
        if(this.library == null){
            this.library = new ArrayList<>();
        }
        System.out.println(this.library);
        sortLibary();
    }

    /**
     * 根据questionNum找题
     * @param questionNum
     * @return
     */
    public Question getByIndex(int questionNum){
        for(Question q : library){
            if(q.getQuestionNum() == questionNum){
                return q;
            }
        }
        return null;
    }

    public void sortLibary(){
        Collections.sort(this.library, Comparator.comparingInt(Question::getQuestionNum));
    }

    public int numOfQuestion(){
        return this.library.size();
    }
}
